package sim1011.poltergeist;

import java.util.ArrayList;
import java.util.HashMap;

import sim1011.nodes.resources.NodeType1;
import sim1011.starter.EctoMobileStarter;
import sim1011.status.Status;

public class TotalExecArbiter extends Arbiter {
	private static final float QOS_WEIGHT = 0.35f;
	private static final float FREE_CPU_WEIGHT = 0.5f;
	private static final float FREE_MEM_WEIGHT = 0.15f;
	private static final long serialVersionUID = 3968477216065815213L;
	protected HashMap<String, Float> finalRank;
	protected Status status;

	public TotalExecArbiter(Status s, float cost) {
		this.cost = cost;
		this.status = s;
	}

	@Override
	public void makeYourDecision(String component, String curContainer,
			ArrayList<String> availableNodes, HashMap<String, Float> finalRank) {
		this.finalRank = finalRank;
		for (String movingNodeName : availableNodes) {
			if(movingNodeName.equals(curContainer)){
				float QoSPerc = 100;
				float averageFreeCpuPerc = status.getAverageFreeCpu(movingNodeName)
						/ status.getResourceManagersNominalCPU()
								.get(movingNodeName).floatValue()*100;
				float averageFreeMemPerc = status
						.getAverageFreeMemory(movingNodeName)
						/ status.getResourceManagersNominalMemory()
								.get(movingNodeName).floatValue()*100;
				/*float newRank = (FREE_CPU_WEIGHT*averageFreeCpuPerc
						+ FREE_MEM_WEIGHT*averageFreeMemPerc + QOS_WEIGHT*QoSPerc);*/
				float newRank = QOS_WEIGHT * QoSPerc +  FREE_CPU_WEIGHT* (100*averageFreeCpuPerc/NodeType1.NOMINAL_CPU) +
						FREE_MEM_WEIGHT * (100*averageFreeMemPerc/NodeType1.NOMINAL_MEMORY);
				float oldRank = finalRank.get(movingNodeName);
				finalRank.put(movingNodeName, this.cost * (newRank) + oldRank);
				
			} else {
				float QoSPerc = ((status.getNetworkMatrix().getLinkQoS(curContainer,
						movingNodeName).floatValue())/(EctoMobileStarter.MAX_NETWORK_LINK_QOS))*100;
				float averageFreeCpuPerc = status.getAverageFreeCpu(movingNodeName)
						/ status.getResourceManagersNominalCPU()
								.get(movingNodeName).floatValue()*100;
				float averageFreeMemPerc = status
						.getAverageFreeMemory(movingNodeName)
						/ status.getResourceManagersNominalMemory()
								.get(movingNodeName).floatValue()*100;
				/*float newRank = (FREE_CPU_WEIGHT*averageFreeCpuPerc7
						+ FREE_MEM_WEIGHT*averageFreeMemPerc + QOS_WEIGHT*QoSPerc);*/
				float newRank = QOS_WEIGHT * QoSPerc +  FREE_CPU_WEIGHT* (100*averageFreeCpuPerc/NodeType1.NOMINAL_CPU) +
						FREE_MEM_WEIGHT * (100*averageFreeMemPerc/NodeType1.NOMINAL_MEMORY);
				float oldRank = finalRank.get(movingNodeName);
				finalRank.put(movingNodeName, this.cost * (newRank) + oldRank);
			}			
		}
	}
}
